import UIKit

private let normalColor = UIColor.clearColor()
private let borderWidth: CGFloat = 1.5
private let indexLabelWH: CGFloat = 20

class ImagePickerTagCell: ImagePickerBaseCell {
    
    var borderColor: UIColor?
    var fillLabelColor: UIColor? {
        willSet {
            if let color = newValue {
                indexLabel.backgroundColor = color
            }
        }
    }
    
    override var albumImage: AlbumImage? {
        willSet {
            imageView.image = newValue?.thumbnail
            indexLabel.hidden = !newValue!.isSelected
            self.layer.borderWidth = borderWidth
            self.layer.borderColor = indexLabel.hidden ? normalColor.CGColor : borderColor?.CGColor
            
            if newValue!.isSelected {
                indexLabel.text = String(newValue!.selectedIndex)
            }
        }
    }
    
    private lazy var indexLabel: UILabel = {
        let indexLabel = UILabel()
        indexLabel.font = UIFont.systemFontOfSize(13)
        indexLabel.textColor = UIColor.whiteColor()
        indexLabel.textAlignment = .Center
        indexLabel.clipsToBounds = true
        
        return indexLabel
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(indexLabel)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        indexLabel.x = width - indexLabelWH - borderWidth
        indexLabel.y = borderWidth
        indexLabel.width = indexLabelWH
        indexLabel.height = indexLabelWH
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
